package com.sxz.sbs.module.sms;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.sxz.sbs.R;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.event.ChoseSmsClientEvent;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.sms.adapter.TagSmsAdapter;
import com.sxz.sbs.module.sms.mode.SmsCustomBean;
import com.sxz.sbs.module.sms.presenter.SendSmsPresenter;
import com.sxz.sbs.module.sms.view.SendSmsView;
import com.sxz.sbs.module.staff.OperateSuccessActivity;
import com.sxz.sbs.router.RouterUrl;
import com.sxz.sbs.widget.CommonTitle;
import com.sxz.sbs.widget.StallAlertDialog;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagFlowLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 群发短信
 */
@Route(path = RouterUrl.SEND_SMS)
public class SendSmsActivity extends BaseActivity<SendSmsPresenter> implements SendSmsView {

    @BindView(R.id.tv_clear_content)
    TextView tvClearContent;
    @BindView(R.id.et_sms_content)
    EditText etSmsContent;
    @BindView(R.id.tv_receive)
    TextView tvReceive;
    @BindView(R.id.tv_sms_contact)
    TagFlowLayout tvSmsContact;
    @BindView(R.id.btn_send_sms)
    Button btnSendSms;
    private List<String> idList = new ArrayList<>();
    private static final int REQUEST_TEMPLATE_CODE = 1001;
    private List<SmsCustomBean> data = new ArrayList<>();
    /**
     * 电话串
     */
    private String phoneString = "";
    private TagSmsAdapter adapter;
    @Override
    protected void init() {
        new CommonTitle.Builder(this).setTitle("短信群发").build().show();
        EventBus.getDefault().register(this);
        adapter = new TagSmsAdapter(this,data);
        tvSmsContact.setAdapter(adapter);
        tvSmsContact.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                tvReceive.setText("接收人："+(data.size()-1));
                idList.remove(data.get(position).getId()+"");
                data.remove(position);
                adapter.notifyDataChanged();
                return false;
            }
        });

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_send_sms;
    }

    @Override
    protected SendSmsPresenter createPresenter() {
        return new SendSmsPresenter();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onFinishChoseEvent(ChoseSmsClientEvent event) {
        Map<Integer,SmsCustomBean> map = event.getChoseMap();
        if (map.size() <= 0) {
            return;
        }
        phoneString = "";
         for (Map.Entry<Integer,SmsCustomBean> entry : map.entrySet()) {

             boolean isHave = false;
             for (int i = 0; i < idList.size(); i++) {
                 if ((entry.getValue().getId()+"").equals(idList.get(i))) {
                     isHave = true;
                     break;
                 }
             }
             if (!isHave) {
                 data.add(entry.getValue());
                 idList.add(entry.getValue().getId()+"");
             }
        }
        adapter.notifyDataChanged();
        tvReceive.setText("接收人："+data.size());
        //tvSmsContact.setText(sbUser.toString().substring(0,sbUser.toString().length()-1));
    }

    @OnClick({R.id.tv_clear_content, R.id.tv_clear_contact, R.id.tv_sms_contact,R.id.tv_sms_template,R.id.tv_client_list, R.id.btn_send_sms})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_clear_content:
                new StallAlertDialog.Builder(this).setMessage("清空短信发送内容？").setPositiveButton(new StallAlertDialog.OnPositiveListener() {
                    @Override
                    public void onPositiveClick(View view) {
                        etSmsContent.setText("");
                    }
                }).create().show();
                break;
            case R.id.tv_sms_template:
                startActivityForResult(new Intent(this,SmsTemplateActivity.class),REQUEST_TEMPLATE_CODE);
                break;
            case R.id.tv_clear_contact://清空短信接收人？
                new StallAlertDialog.Builder(this).setMessage("清空短信接收人？").setPositiveButton(new StallAlertDialog.OnPositiveListener() {
                    @Override
                    public void onPositiveClick(View view) {
                        data.clear();
                        adapter.notifyDataChanged();
                        phoneString = "";
                        idList.clear();
                        tvReceive.setText("接收人：0");
                    }
                }).create().show();
                break;
            case R.id.tv_sms_contact:
                break;
            case R.id.tv_client_list:
                startActivity(SmsClientActivity.class);
                break;
            case R.id.btn_send_sms:
                if (TextUtils.isEmpty(etSmsContent.getText().toString())) {
                    ToastUtils.showShort("短信内容不能为空");
                    return;
                }
                if (data.size() <= 0) {
                    ToastUtils.showShort("请选择接收人");
                    return;
                }
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < data.size(); i++) {
                    sb.append(data.get(i).getId()+",");
                }
                phoneString = sb.toString();
                phoneString = phoneString.substring(0,phoneString.length()-1);
                LogUtils.e(phoneString);
                HashMap<String,Object> map = HttpParamMap.getParamMap();
                map.put("content",etSmsContent.getText().toString());
                map.put("ids",phoneString);
                mPresenter.sendSms(map);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data == null) {
            return;
        }
        if (requestCode == REQUEST_TEMPLATE_CODE) {
            String smsContent = data.getStringExtra("sms_content");
            etSmsContent.setText(smsContent);
            etSmsContent.setSelection(smsContent.length());
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onSendSmsSuccess(String bean) {
        startActivity(OperateSuccessActivity.class);
        finish();
    }
}
